package com.skytech.component.crawler.quartz;

import com.skytech.component.crawler.quartz.QuartzJob;
import com.skytech.component.crawler.quartz.QuartzManager;
import com.skytech.component.crawler.scratch.QuartzConfig;
import com.skytech.component.crawler.scratch.QuartzConfigDAO;
import org.quartz.JobDataMap;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ScheduleRefresh implements CommandLineRunner{



    @Autowired
    private QuartzConfigDAO quartzConfigDAO;



    @Autowired
    private QuartzManager quartzManager;

    /**
     * 定时任务随着程序的启动自启
     * @param args
     */
    @Override
    public void run(String... args) {
        List<QuartzConfig> quartzConfigList=quartzConfigDAO.findAll();
        JobDataMap jobDataMap=new JobDataMap();
        for (QuartzConfig quartzConfig:quartzConfigList) {
            quartzManager.addJob(quartzConfig.getRuleId(),quartzConfig.getRuleId(),quartzConfig.getRuleId(),quartzConfig.getRuleId(),QuartzJob.class,quartzConfig.getCron(),jobDataMap,quartzConfig.getRuleId());
        }

    }




}
